﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.Car;

namespace S4Store.Controllers
{
    public class CarBrandController : BaseController
    {

        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            CarBrandListPage model = new CarBrandListPage();
            if (this.CurrentUser.Position == Position.GeneralManager)
            {
                model.AllowAdd = true;
                model.AllowModify = true;
                model.AllowChangeStatus = true;
            }
            if (this.CurrentUser.Position == Position.SystemManager)
            {
                model.AllowAdd = true;
                model.AllowDelete = true;
                model.AllowModify = true;
                model.AllowChangeStatus = true;
            }
            return View(model);            
        }

        public ActionResult Query()
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                if (
                           CurrentUser.Position == S4Store.Domain.Position.GeneralManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager
                           )
                {
                    var data = CarService.GetCarBrandQuery().OrderBy(x => x.PinPaiId).ToList();
                    return View(data);
                }
                else
                {
                    var data = CarService.GetEnableCarBrand(CurrentUser.Store.PinPaiId).OrderBy(x => x.PinPaiId).ToList();
                    return View(data);

                }
            }
        }

        public ActionResult Add()
        {
            List<S4Store.Domain.PinPai> pinpais = new List<PinPai>();
            using (var t = new Transtion())
            {
                t.Inject(CarService);

                if (
                           CurrentUser.Position == S4Store.Domain.Position.GeneralManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager
                           )
                {
                    pinpais = CarService.GetEnablePinPai().ToList();
                }
                else
                {
                    pinpais = CarService.GetEnablePinPai().Where(x => x.Id == CurrentUser.Store.PinPaiId).ToList();
                }
                t.Commit();

                return View(pinpais);
            } 
        }

        [HttpPost]
        public JsonResult Add(CarBrand model)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                CarService.AddCarBrand(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success=true
                    }
                };
            } 
        }

        [HttpPost]
        public ActionResult Modify(int id)
        {
            Combin<CarBrand, List<S4Store.Domain.PinPai>> model = new Combin<CarBrand, List<PinPai>>();
            using (var t = new Transtion())
            {
                t.Inject<CarService>(CarService);
                model.Data1 = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == id);
                
                if (
                           CurrentUser.Position == S4Store.Domain.Position.GeneralManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager
                           )
                {
                    model.Data2 = CarService.GetEnablePinPai().ToList();
                }
                else
                {
                    model.Data2 = CarService.GetEnablePinPai().Where(x => x.Id == CurrentUser.Store.PinPaiId).ToList();
                }
                t.Commit();
                if (model != null)
                {
                    return View(model);
                }
                return this.Content("车型品牌不存在");
            }   
            
        }


        [HttpPost]
        public JsonResult ModifyBack(CarBrand model)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                CarService.ModifyCarBrand(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }

        }

        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            CheckPower(Position.SystemManager);
            if(Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    CarService.DeleteCarBrand(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Upline(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要上线的品牌");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    var model = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = false;
                        CarService.ModifyCarBrand(model);

                        foreach (var carModel in model.CarModels)
                        {
                            carModel.IsClose = false;
                            CarService.ModifyCarModel(carModel);
                        }
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "操作成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Downline(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要下线的品牌");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    var model = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = true;
                        CarService.ModifyCarBrand(model);

                        foreach (var carModel in model.CarModels)
                        {
                            carModel.IsClose = true;
                            CarService.ModifyCarModel(carModel);
                        }
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "操作成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }
}
